<template>
  <div>
    <Row>
      <Col span="24">
        <Card class="margin-bottom-10">
          <Form inline>
            <FormItem class="margin-bottom-0">
              <Select v-model="searchConf.game_hash" filterable remote :remote-method="getGameName" style="width:260px"
                      placeholder='请选择游戏' :loading="nameLoading">
                <Option v-for="(option, index) in gameNameOption" :value="option.value" :key="index">{{ option.label }}</Option>
              </Select>
            </FormItem>
            <FormItem class="margin-bottom-0">
              <Input v-model="searchConf.openid" :clearable="true" style="width:260px" placeholder="请输入OpenId" />
            </FormItem>
            <FormItem class="margin-bottom-0">
              <Select v-model="searchConf.type" filterable style="width:160px" :clearable="true"
                      placeholder='请选择使用状态'>
                <Option value="1">消费</Option>
                <Option value="2">获得</Option>
                <Option value="3">订单充值</Option>
              </Select>
            </FormItem>
            <FormItem class="margin-bottom-0">
              <DatePicker class="margin-right-10" :type="'daterange'" placeholder="请选择日期" style="width: 220px"
                          separator=" ~ " format="yyyy-MM-dd" :value="searchConf.date" @on-change="setFormDate"
                          :transfer="true" />
            </FormItem>
            <FormItem class="margin-bottom-0">
              <div style="width:120%;display: flex;justify-content: space-around;">
                <Button type="primary" @click="search">{{ $t('find_button') }}/{{ $t('refresh_button') }}</Button>
                <Button type="warning" @click="download">导出</Button>
              </div>
            </FormItem>
          </Form>
        </Card>
      </Col>
    </Row>
    <Row style="margin-top: 15px">
      <Col span="24">
        <Card>
          <div>
            <Table :loading="listLoading" :columns="columnsList" :data="tableData" border disabled-hover />
          </div>
          <div class="margin-top-15" style="text-align: center">
            <Page :total="tableShow.listCount" :current="tableShow.currentPage" :page-size="tableShow.pageSize"
                  @on-change="changePage" @on-page-size-change="changeSize" show-elevator show-sizer show-total />
          </div>
        </Card>
      </Col>
    </Row>
  </div>
</template>
<script>
import { getList, exportList } from '@/api/game-coin'
import { getChoose } from '@/api/game-manager'
export default {
  name: 'operate-order',
  data () {
    return {
      nameLoading: false,
      gameNameOption: [],
      gameShow: {},
      columnsList: [
        {
          title: '序号',
          type: 'index',
          width: 65,
          align: 'center'
        },
        {
          title: '所属游戏',
          align: 'center',
          render: (h, params) => {
            this.getGameName()
            return h('span', this.gameShow[params.row.game_hash])
          },
          width: 120
        },
        {
          title: 'openid',
          align: 'center',
          key: 'openid',
          tooltip: true,
          minWidth: 280
        },
        {
          title: '角色ID',
          align: 'center',
          key: 'role_id',
          tooltip: true,
          width: 120
        },
        {
          title: '使用状态',
          align: 'center',
          render: (h, params) => {
            if (params.row.type === 1) {
              return h('span', '消费')
            } else if (params.row.type === 2) {
              return h('span', '获得')
            } else if (params.row.type === 3) {
              return h('span', '订单充值')
            }
          },
          width: 120
        },
        {
          title: '点券数量',
          align: 'center',
          key: 'coin',
          minWidth: 120
        },
        {
          title: '消耗or获得前的点券余额',
          align: 'center',
          key: 'coin_balance',
          minWidth: 120
        },
        {
          title: '使用来源',
          align: 'center',
          key: 'source_type',
          minWidth: 80
        },
        {
          title: '来源id',
          align: 'center',
          key: 'source_id',
          minWidth: 80
        },
        {
          title: '来源次数',
          align: 'center',
          key: 'source_num',
          minWidth: 80
        },
        {
          title: '来源描述',
          align: 'center',
          key: 'source_desc',
          minWidth: 120
        },
        {
          title: '记录时间',
          align: 'center',
          key: 'created_at',
          width: 200
        }
      ],
      tableData: [],
      tableShow: {
        currentPage: 1,
        pageSize: 10,
        listCount: 0
      },
      searchConf: {
        game_hash: '',
        openid: '',
        type: '',
        date: []
      },
      modalSetting: {
        show: false,
        loading: false,
        index: 0
      },
      buttonShow: {
        edit: true
      },
      listLoading: false,
      total: {},
      userInfo: {}
    }
  },
  created () {
    let vm = this
    vm.getList()
    vm.getGameName()
  },
  methods: {
    getGameName (value) {
      if (!this.gameNameOption.length) {
        getChoose({
          keyword: value
        }).then(response => {
          this.gameNameOption = response.data.data.list
          this.gameNameOption.forEach(i => {
            this.gameShow[i.value] = i.label
          })
        })
      }
    },
    setFormDate: function (time) {
      this.searchConf.date = time
    },
    changePage (page) {
      this.tableShow.currentPage = page
      this.getList()
    },
    changeSize (size) {
      this.tableShow.pageSize = size
      this.getList()
    },
    search () {
      this.tableShow.currentPage = 1
      this.getList()
    },
    download () {
      let d = new Date(new Date().getTime())
      let date = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds()
      let fileName = `点券-${date}.xlsx`
      exportList({
        ...this.searchConf
      }).then(response => {
        if (response.data.byteLength === 31) {
          this.$Message.error('导出失败,暂无数据')
          return
        }
        if (response.data.byteLength === 32) {
          this.$Message.error('导出失败,数据超过1000条,请联系管理员')
          return
        }
        const data = new Blob([response.data], { type: 'application/octet-stream' })
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          // IE workaround
          window.navigator.msSaveBlob(data, fileName)
        } else {
          // Other browsers
          const url = window.URL.createObjectURL(data)
          const link = document.createElement('a')
          link.href = url
          link.setAttribute('download', fileName)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        }
      })
    },
    getList () {
      let vm = this
      let params = {
        page: vm.tableShow.currentPage,
        size: vm.tableShow.pageSize,
        ...vm.searchConf
      }
      vm.listLoading = true
      getList(params).then(response => {
        vm.tableData = response.data.data.list
        vm.total = response.data.data.total
        if (response.data.data.userInfo) {
          vm.userInfo = response.data.data.userInfo
        }
        vm.tableShow.listCount = response.data.data.count
        vm.listLoading = false
      })
    }
  }
}
</script>
